<?php
/**
 * Created by PhpStorm.
 * User: yangkang
 * Date: 2018/3/14
 * Time: 下午3:08
 */

namespace App\Services\Mobile;


use App\Repositories\UserRepository;
use Illuminate\Support\Facades\Request;

class UserService
{
    private $userRepository;

    public function __construct(UserRepository $userRepository)
    {
        $this->userRepository = $userRepository;
    }

    public function store($user)
    {
        $user['create_time'] = date('Y-m-d H:i:s');
        $user['create_ip'] = Request::getClientIP();
        $user['salt'] = str_random(8);

        $this->userRepository->store($user);
    }

    public function auth($user)
    {
        $password = $this->userRepository->getPasswordByMobile($user['mobile']);
        if ($password == md5($user['password'])) {
            session(['mobile' => $user['mobile']]);
            return true;
        } else {
            return false;
        }
    }

    public function logout()
    {
        session()->flush();
    }

}